package com.example.gkcircle.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.gkcircle.R;
import com.example.gkcircle.library.object.Book;

import java.util.List;

/**
 * 搜索图书
 * Created by Jehu on 16/3/19.
 */
public class BorrowAdapter extends BaseAdapter{


    private List<Book> mData;
    private Activity mActivity;
    private LayoutInflater inflater;

    public BorrowAdapter(Activity mActivity, List<Book> data) {
        this.mData=data;
        this.mActivity = mActivity;
        this.inflater = LayoutInflater.from(mActivity);
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
        TextView id;
        TextView bookname;
        TextView author;
        TextView borrowedAt;
        TextView returnAt;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=inflater.inflate(R.layout.library_borrow_item,null);
            viewHolder.id=(TextView)convertView.findViewById(R.id.library_borrow_item_id);
            viewHolder.bookname=(TextView)convertView.findViewById(R.id.library_borrow_item_bookname);
            viewHolder.author=(TextView)convertView.findViewById(R.id.library_borrow_item_author);
            viewHolder.borrowedAt=(TextView)convertView.findViewById(R.id.library_borrow_item_borrowedat);
            viewHolder.returnAt=(TextView)convertView.findViewById(R.id.library_borrow_item_returnat);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
        final Book book=mData.get(position);
        viewHolder.id.setText((position+1)+"");
        viewHolder.bookname.setText(book.getBookName());
        viewHolder.author.setText("作者："+book.getAuthor());
        viewHolder.borrowedAt.setText("借阅日期："+book.getBorrowedAt());
        viewHolder.returnAt.setText("归还日期："+book.getReturnAt());

        return convertView;
    }
}
